package com.turnengine.client.global.user.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Login User Data Serializer.
 */
public class LoginUserDataSerializer extends AbstractCommandDataSerializer<Long> implements ILoginUserDataSerializer {

	/**
	 * Creates a new Login User Data Serializer.
	 */
	public LoginUserDataSerializer() {
		super(LoginUser.COMMAND_ID);
	}

	@Override
	public Long readReturnValue(IDataReader reader) throws IOException {
		Long value = reader.readLongObject(true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, Long value) throws IOException {
		writer.writeLongObject(value, true);
	}

	@Override
	public IExecutableCommand<Long> readRequest(IDataReader reader) throws IOException {

		// Read fields
		String nameOrEmail = reader.readString(true);
		String password = reader.readString(true);
		int address = reader.readInt();

		// Create object
		LoginUser object = new LoginUser();
		object.setNameOrEmail(nameOrEmail);
		object.setPassword(password);
		object.setAddress(address);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<Long> argument) throws IOException {
		LoginUser object = (LoginUser) argument;

		// Get fields
		String nameOrEmail = object.getNameOrEmail();
		String password = object.getPassword();
		int address = object.getAddress();

		// Write fields
		writer.writeString(nameOrEmail, true);
		writer.writeString(password, true);
		writer.writeInt(address);
	}

	@Override
	public IExecutableCommandResponse<Long> newResponse() {
		return new LoginUserResponse();
	}
}
